package com.ljm.handle;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class ScriptHandle {


    /**
     * 返回执行的shell命令
     * @param shells
     * @return
     */
    private synchronized static String resLinuxScript(List<String> shells) {
        String FILE_PATH = "/root/temp_shell.sh";
        // 以本地创建文件的方式
        try {
            FileWriter fileWriter = new FileWriter(FILE_PATH, false);
            for (String shell : shells) {
                fileWriter.append(shell + "\n");
            }
            fileWriter.flush();
            fileWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        StringBuilder strBuilder = new StringBuilder();
        try {
            InputStream in = null;
            Runtime.getRuntime().exec("chmod 777 " + FILE_PATH);
            Process pro = Runtime.getRuntime().exec(FILE_PATH);
            pro.waitFor(5, TimeUnit.SECONDS);
            in = pro.getInputStream();
            BufferedReader read = new BufferedReader(new InputStreamReader(in));

            String result = "";
            while (result != null) {
                result = read.readLine();
                if (result != null) {
                    strBuilder.append(result);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strBuilder.toString();
    }

    /**
     * 获取镜像id
     * @return
     */
    private static List<String> getDockerImageId() {
        List<String> res;
        List<String> strings = new ArrayList<>();
        strings.add("docker images  | awk '{ print $3}' | tail -n +2");
        String resLinuxScript = resLinuxScript(strings);
        String[] split = StrUtil.split(resLinuxScript, 12);
        res = new ArrayList<>(Arrays.asList(split));
        return res;
    }

    public static void runScript() {

        // 特殊处理：blade-tcp
        String selectTcpCmd = "docker images |grep tcp| awk '{ print $3}' | tail -n +1";
        List<String> shells5 = new ArrayList<>();
        shells5.add(selectTcpCmd);
        String tcpImages = resLinuxScript(shells5);


        // 特殊处理：blade-gateway
        String selectCmd6 = "docker images |grep gateway| awk '{ print $3}' | tail -n +1";
        List<String> shells6 = new ArrayList<>();
        shells6.add(selectCmd6);
        String images6 = resLinuxScript(shells6);


        // 特殊处理：springboot-admin
        String selectCmd7 = "docker images |grep admin| awk '{ print $3}' | tail -n +1";
        List<String> shells7 = new ArrayList<>();
        shells6.add(selectCmd7);
        String images7 = resLinuxScript(shells7);


        // 特殊处理：magic
        String selectCmd8 = "docker images |grep device-connect| awk '{ print $3}' | tail -n +1";
        List<String> shells8 = new ArrayList<>();
        shells6.add(selectCmd8);
        String images8 = resLinuxScript(shells8);


        List<String> dockerImageId = getDockerImageId();
        for (String imageId : dockerImageId) {

            String port = "    ";
            if (imageId.equals(tcpImages)) {
                port = " -p 10222:10222 ";
            }

            if (imageId.equals(images6)) {
                port = " -p 8089:8089 ";
            }

            if (imageId.equals(images7)) {
                port = " -p 7002:7002 ";
            }

            if (imageId.equals(images8)) {
                port = " -p 9999:9999 ";
            }


            String runCmd = "docker run -itd  " + port + "   --log-driver json-file --log-opt max-size=500m  --log-opt max-file=5   " + imageId;
            System.out.println("容器启动:" + imageId);
            List<String> shells4 = new ArrayList<>();
            shells4.add(runCmd);
            resLinuxScript(shells4);
            ThreadUtil.sleep(12, TimeUnit.SECONDS);
        }


    }
}
